package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 请求参数接收
 *
 * @author stone
 * @date 2021/11/22 17:00
 */
@RestController
@RequestMapping("user")
public class UserController {

    //localhost:8080/user/register?username=songge&age=30&gender=male&married=true
    //上述请求参数：username、age、gender、married
    //请求参数名和Handler方法的形参名一致
    @RequestMapping("register")
    public BaseRespVo register(String username, String age, String gender, String married) {
        return BaseRespVo.ok(null);
    }

    //修改了形参的类型为：基本类型、包装类 👉 也可以直接接收
    //SpringMVC 👉 Converter
    @RequestMapping("register2")
    public BaseRespVo register2(String username, Integer age, String gender, boolean married) {
        return BaseRespVo.ok(null);
    }
    //http://localhost:8080/user/register3?username=songge&gender=male&married=true
    //建议使用包装类 👉 更强壮
    @RequestMapping("register3")
    public BaseRespVo register3(String username, int age, String gender, boolean married) {
        return BaseRespVo.ok(null);
    }

    //http://localhost:8080/user/register4?username=songge&hobbies=sing&hobbies=dance&hobbies=rap
    //增加数组参数
    @RequestMapping("register4")
    public BaseRespVo register4(String username, String[] hobbies) {
        return BaseRespVo.ok(hobbies);
    }
    //http://localhost:8080/user/register5?username=songge&birthday=1991-11-22
    //http://localhost:8080/user/register5?username=songge&birthday=1991/11/22
    //SpringMVC提供了对应的转换器Converter 👉 但是日期的默认格式是yyyy/MM/dd
    @RequestMapping("register5")
    public BaseRespVo register5(String username, Date birthday) {
        return BaseRespVo.ok(null);
    }

    //http://localhost:8080/user/register6?username=songge&birthday=1991-11-22
    //如果使用的不是默认的格式可以不？可以，但是需要我们自行指定日期格式
    @RequestMapping("register6")
    public BaseRespVo register6(String username,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {
        return BaseRespVo.ok(null);
    }

    @Autowired
    ConfigurableConversionService conversionService;

    //http://localhost:8080/user/register7?username=songge&birthday=1991-11-22
    //如果使用的不是默认的格式可以不？可以，但是需要我们自行指定日期格式
    @RequestMapping("register7")
    public BaseRespVo register7(String username,
                                Date birthday) {
        return BaseRespVo.ok(null);
    }
}
